#!/bin/bash

libfile='http-1.1.bsh';

{ # $libfile may be located in path...

    { source "$libfile" || source ../$libfile; } 2>/dev/null || { 
	echo "$0: cannot locate required module 'http-1.1.bsh'" >&2;
	exit 1;
    }

}

function knownCommand() {
    type "$1" 2>/dev/null >/dev/null;
}

host=`http.parse.uri host $1` || {
    echo "$0: invalid uri host: '$1'";
    exit 2;
} >&2;

port=`http.parse.uri port $1` || port=80;

http.std.get $1 | netcat $host $port | http.stash.header >test.out;

read -n1 -p "Would you like to see the server response header? y, n: " ans;
echo

[[ $ans = y ]] && http.raid.stash;
echo 

read -n1 -p "Would you like to view the file? y, n: " ans;
echo

[[ $ans = y ]] && {

    echo
    read -p "Viewer command name or path: " cmd;
    echo

    knownCommand $cmd || {
	echo "$0: invalid shell command: '$cmd test.out'" >&2;
	exit 1;
    }

    $cmd test.out 2>/dev/null || echo "$0: launching '$cmd' with recieved content failed" >&2;

}

rm test.out;
